import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author 陈Ser
 * @ClassName Server
 * @description: TODO
 * @Date 2021/10/11 23:42
 * @Version 1.0
 */
public class Fsfcsf implements Runnable {

    private OutputStream outputStream;
    private InputStream inputStream;
    private Socket socket;

    private Fsfcsf() {
    }


    public Fsfcsf(Socket socket) {
        this.socket = socket;
    }

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost(), 3307);
        Fsfcsf fsfcsf = new Fsfcsf(socket);
        fsfcsf.initIO();
        fsfcsf.inPutFile("Tem/src/c.png");
        fsfcsf.close();
    }

    public boolean initIO() {
        try {
            outputStream = socket.getOutputStream();
            inputStream = socket.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }

    public boolean outPutFile(String outPutFilePATH) {
        FileInputStream fileInputStream = null;


        /**
         * new FileInputStream
         */
        try {
            fileInputStream = new FileInputStream(outPutFilePATH);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        }


        /**
         * new byte[]
         */
        byte[] bytes;
        try {
            bytes = new byte[fileInputStream.available()];

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }


        /**
         * TODOSome
         */
        try {

            for (int i = 0; fileInputStream.available() > 0; i++) {
                fileInputStream.read(bytes);
                outputStream.write(bytes, 0, bytes.length);
            }
            socket.shutdownOutput();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }


        System.out.println("Network ends at file output");
        return true;
    }

    public boolean inPutFile(String inPutFilePATH) {
        FileOutputStream fileInputStream = null;


        /**
         * new FileInputStream
         */
        try {
            fileInputStream = new FileOutputStream(inPutFilePATH);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        }


        /**
         * new byte[]
         */
        int len;
        byte[] bytes;
        try {
            /*byteItem = (byte) inputStream.read();
            bytes = new byte[inputStream.available() + 1];
            bytes[0] = byteItem;
            while (inputStream.available() > 0) {
                fileInputStream.write(bytes, 0, bytes.length);
            }*/

            bytes = new byte[1024];
            while ((len = inputStream.read(bytes)) > 0) {
                fileInputStream.write(bytes, 0, len);
            }

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        System.out.println("Network ends at file input");
        return true;
    }

    public void close() {
        try {
            outputStream.close();
            inputStream.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        this.initIO();
//        this.outPutFile("Tem/src/b.png");
        this.inPutFile("Tem/src/c.png");

        this.close();
    }

}
